<?php

namespace App\Http\Controllers\Admin;

use App\Repository\Order\OrderRepository as model;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as BaseController;
use App\Repository\UploadRepository;
use App\Libs\PhpOffice\Excel;
use App\Repository\Shop\SuppilerRepository as supp;
use App\Repository\Order\OrderLogRepository as logModel;
use App\Repository\Order\OrderInfoRepository as infoModel;
use App\Repository\UpThree\MainOrderRepostition;
use App\Jobs\OrderSubmit;
use Illuminate\Support\Facades\DB;

// 分销商
class OrderInfoController extends BaseController
{
  public function __construct(model $model, supp $supp, logModel $logModel, infoModel $infoModel)
  {
    $this->repoist = $model;
    $this->disModel = $supp;
    $this->logModel = $logModel;
    $this->infoModel = $infoModel;
  }
  // 根据info 查询订单数据
  public function index(Request $request)
  {
    $where = [];
    $isTrueAll = false;
    $shop_id = $request->user->shop_id;
    if ($request->has('status') && $request->get('status') != '') {
      $where['where']['order.status'] = $request->get('status');
    }
    if ($request->has('source') && $request->get('source') != '') {
      $where['where']['order.source'] = $request->get('source');
    }

    // if (!empty($request->user->role_id) && $request->user->role_id != 1 && empty($request->get('is_dist'))) {
    //   $where['where']['order.shop_id'] = $request->user->shop_id;
    // }
    if ($request->has('three_msg') && $request->get('three_msg') != '') {
      $where['like']['order.three_msg'] = $request->get('three_msg');
    }
    if ($request->has('sell_name') && $request->get('sell_name') != '') {
      $where['where']['order.sell_name'] = $request->get('sell_name');
    }
    if (!empty($request->get('is_dist'))) {
      $where['like']['order.shop_str'] = '.' . $request->user->shop_id . '.';
    }
    if (($request->has('shop_id')) && $request->get('shop_id') == 0) {
      // $where['where']['order.shop_id'] = $request->user->shop_id;
      $isTrueAll = true;
    }
    if (!empty($request->get('shop_id'))) {
      $where['like']['order.shop_id_str'] = '.' . $request->user->shop_id . '.' . $request->get('shop_id') . '.';
    }
    // var_dump($where, 111111);
    if (!empty($request->get('orderno'))) {
      $where['where']['order.orderno'] = trim($request->get('orderno'), ' ');
    }
    if (!empty($request->get('pay_identity'))) {
      $where['where']['order.pay_identity'] = trim($request->get('pay_identity'), ' ');
    }
    if (!empty($request->get('sup_orderno'))) {
      $where['where']['order.sup_orderno'] = $request->get('sup_orderno');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (!empty($request->get('dist_orderno'))) {
      $where['where']['order.dist_orderno'] = $request->get('dist_orderno');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (!empty($request->get('iccid'))) {
      $where['where']['order.iccid'] = $request->get('iccid');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (!empty($request->get('express_number'))) {
      $where['where']['order.express_number'] = $request->get('express_number');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (!empty($request->get('contact_phone'))) {
      $where['where']['order.contact_phone'] = $request->get('contact_phone');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (!empty($request->get('contact_name'))) {
      $where['where']['order.contact_name'] = $request->get('contact_name');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    // if (!empty($request->get('goods_id')) && empty($request->get('is_dist'))) {
    //   $where['where']['order.goods_id'] = $request->get('goods_id');
    //   // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    // }
    if (!empty($request->get('goods_id')) && empty($request->get('shop_id'))) {
      // $where['where']['order.goods_id'] = $request['goods_id'];
      $where['whereors'] = [
        'where' => ['order.goods_id' => $request->get('goods_id')],
        'whereor' => [['order.sup_goods_id_str', 'like', '%.' . $request->get('goods_id') . '.%']]
      ];
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (!empty($request->get('goods_id')) && !empty($request->get('shop_id'))) {
      $where['like']['order.sup_goods_id_str'] = '%.' . $request->get('goods_id') . '.%';
    }
    // if (!empty($request->get('goods_id')) && !empty($request->get('is_dist'))) {
    //   $where['where']['order.sup_goods_id'] = $request->get('goods_id');
    //   // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    // }
    if (!empty($request->get('product_id'))) {
      $where['where']['order.product_id'] = $request->get('product_id');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (!empty($request->get('channel_id'))) {
      $where['where']['order.sup_id'] = $request->get('channel_id');
    }
    if (!empty($request->get('author_id'))) {
      $where['where']['order.author_id'] = $request->get('author_id');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (($request->has('is_trun')) && ($request->get('is_trun') != '')) {
      $where['where']['order.is_trun'] = $request->get('is_trun');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (!empty($request->get('number'))) {
      $where['like']['order.name'] = $request->get('number');
    }
    $timeType = 'created_at';
    if (!empty($request->get('time_type'))) {
      switch ($request->get('time_type')) {
        case 1:
          $timeType = 'order.created_at';
          break;
        case 2:
          $timeType = 'order_info.three_time_at';
          break;
        case 3:
          $timeType = 'order_info.deliver_at';
          break;
        case 4:
          $timeType = 'order_info.activat_at';
          break;
      }
    }
    //时间验证 
    if (!empty($request->get('dateRange'))) {
      // $time = $request->get('dateRange');
      $where['between'] = [$timeType => $request->get('dateRange')];
    }
    // var_dump($where);
    $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
    $where['limit']['number'] = $request->get('pageSize');
    // DB::connection()->enableQueryLog();
    // $where['join'][] = [];
    $where['join'][] = [
      'table' => 'order_info',
      'left'  => 'order_id',
      'right' => 'order.id'
    ];
    if ($isTrueAll) {
      $where['whereor'] = [
        'where' => ['order.shop_id' => $shop_id],
        'whereor' => [['order.shop_str', 'like', '%.' . $shop_id . '.%']]
      ];
    }

    $where['select'] = ['order.*', 'order_info.activat_at', 'order_info.deliver_at', 'order_info.three_time_at'];
    $data =  $this->repoist->index($where);
    // $log = DB::getQueryLog();
    // var_dump($log);
    return $data;
  }
  // 根据info 查询订单数据
  public function indexDist(Request $request)
  {
    $request->offsetSet('is_dist', 1);
    $res =  $this->index($request);
    // $data['']
    if (!empty($res['data'])) {
      foreach ($res['data'] as $key => &$v) {
        $resSouc = explode('.', $v['shop_name_str']);
        $resSouId = explode('.', $v['shop_id_str']);
        $v['source'] = '';
        if (empty($resSouId)) {
          continue;
        }
        foreach ($resSouId as $keys => $val) {
          if (!empty($val) && $val == $request->get('user')->shop_id) {
            $v['source'] = !empty($resSouc[($keys + 1)]) ? $resSouc[($keys + 1)] : $v['shop_name'];
          }
        }
        if ($resSouId[1] != $request->get('user')->shop_id) {
          $v['channel_error_josn'] = '[]';
        }
      }
    }
    return $res;
  }
}
